package com.example.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.Articles;
import com.example.entity.NewsEntity;
import com.example.entity.TextEntity;
import com.example.entity.item;
import com.example.util.CheckUtil;
import com.example.util.HttpClientUtil;
import com.example.util.WxMessageUtil;

import me.chanjar.weixin.mp.util.xml.XStreamTransformer;

@Controller
public class IndexController {
	@RequestMapping(value="vx",method=RequestMethod.GET)
	public void vx(HttpServletRequest request,HttpServletResponse response) {
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		PrintWriter out = null;
		try {
			  out = response.getWriter();
			if(CheckUtil.checkSignature(signature, timestamp, nonce)){
				out.write(echostr);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			out.close();
		}
		
	}
	
	/*@RequestMapping(value="vx",method=RequestMethod.POST)
	public String messageVx(HttpServletRequest request,HttpServletResponse response) {
		try {
			String xml = null;
			Map<String, String> messageMap = WxMessageUtil.parseXml(request);
			if (WxMessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(messageMap.get("MsgType"))) {
				response.setCharacterEncoding("utf8");
				TextEntity text = new TextEntity(messageMap,"");
				String contentUrl = messageMap.get("Content");
				String tbkl = HttpClientUtil.jxTb(contentUrl);
				if(tbkl != null) {
					String json = HttpClientUtil.acquireId(tbkl);
					//获取返回的Id
					JSONObject js = JSON.parseObject(json);
					String yhqxx = HttpClientUtil.yhq(js.getString("data"));
					//获取商品属性
					JSONObject jsonObject = JSONObject.parseObject(yhqxx).getJSONObject("result").getJSONObject("data");
					if (jsonObject.getBooleanValue("has_coupon")) {
						String content = "您搜索的商品优惠券为"
										+jsonObject.getString("coupon_info")
										+"优惠券信息为短链接为"+
										jsonObject.getString("short_url")
										+"淘口令为"+jsonObject.getString("tpwd");
						text.setContent(content);
					}else {
						String content = "您搜索的商品暂无优惠券"
								+"下单地址"+jsonObject.getString("item_url");
						text.setContent("未查到优惠券信息"+content);
					}
					xml = WxMessageUtil.objectToXml(text);
				}else {
					text.setContent( "我还没长大听不懂你说什么");
					xml = WxMessageUtil.objectToXml(text);
				}
				response.getWriter().println(xml);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}*/
	
	@RequestMapping(value="vx",method=RequestMethod.POST)
	public String messageVx(HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setCharacterEncoding("utf8");
			NewsEntity news = null;
			String xml = null;
			String texttxt = null;
			Map<String, String> messageMap = WxMessageUtil.parseXml(request);
			/**
			 * 关注事件
			 */
			if(WxMessageUtil.SUBSCRIBE.equals(messageMap.get("Event"))) {
				StringBuffer Content = new StringBuffer();
				Content.append("❤❤❤❤❤❤❤❤❤❤❤\n");
				Content.append("❤小主，终于等到你     ❤\n");
				Content.append("❤                               ❤\n");
				Content.append("❤网购不花冤枉钱，请发送❤\\n淘宝分享链接给我试试我的威力吧！！！ ❤\n");
				Content.append("❤                   ❤\n");
				Content.append("❤使用教程              ❤\n");
				Content.append("❤                       ❤\n");
				Content.append("❤点击分享-->复制链接      ❤\n");
				Content.append("❤                          ❤\n");
				Content.append("❤把它发给我，我就可以帮您❤\n查找优惠券啦！！！ ❤\n");
				Content.append("❤                                    ❤\n");
				Content.append("❤●ω●  ≥▂≤         ❤\n");
				Content.append("❤❤❤❤❤❤❤❤❤❤❤❤\n");
				TextEntity textEntity = new TextEntity(messageMap, Content.toString());
				String EventText = WxMessageUtil.objectToXml(textEntity);
				System.out.println(EventText);
				response.getWriter().println(EventText);
			}
			if (WxMessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(messageMap.get("MsgType"))) {
				TextEntity text = new TextEntity(messageMap,"");
				String contentUrl = messageMap.get("Content");
				String tbkl = HttpClientUtil.jxTb(contentUrl);
				if(tbkl != null) {
					String json = HttpClientUtil.acquireId(tbkl);
					//获取返回的Id
					JSONObject js = JSON.parseObject(json);
					String yhqxx = HttpClientUtil.yhq(js.getString("data"));
					String detail = HttpClientUtil.detail(js.getString("data"));
					JSONObject detailJson = JSONObject.parseObject(detail).getJSONObject("data").getJSONObject("n_tbk_item");
					String imageUrl =detailJson.getString("pict_url");
					String title = detailJson.getString("title");
					//获取商品属性
					JSONObject jsonObject = JSONObject.parseObject(yhqxx).getJSONObject("result").getJSONObject("data");
					if (jsonObject.getBooleanValue("has_coupon")) {
						item item = new item("已为您找到"+title+"信息", "优惠券信息"+jsonObject.getString("coupon_info")+"                    点击消息领取优惠券", imageUrl, jsonObject.getString("short_url"));
						Articles articles = new Articles(item);
						news = new NewsEntity(messageMap,"1", articles, item);
						/*String content = "您搜索的商品优惠券为"
										+jsonObject.getString("coupon_info")
										+"优惠券信息为短链接为"+
										jsonObject.getString("short_url")
										+"淘口令为"+jsonObject.getString("tpwd");
						text.setContent(content);*/
						
						xml = WxMessageUtil.objectToXml(news);
					}else {
						String content = "您搜索的商品暂无优惠券"
								+"下单地址"+jsonObject.getString("item_url");
						text.setContent("未查到优惠券信息"+content);
						xml = WxMessageUtil.objectToXml(text);
					}
				}else {
					text.setContent("请发送淘宝分享商品链接我来帮您查找优惠券");
					xml = WxMessageUtil.objectToXml(text);
				}
				response.getWriter().println(xml);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
